package cn.rui.common.filter;

import cn.rui.common.Result;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 过滤器,拦截登录方法
 * @author 徽州大都督
 * @date 2021/4/19
 */
@Component
public class CodeFilter extends GenericFilterBean{

    private String defaultFilterProcessUrl = "/login";

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;

        response.setCharacterEncoding("UTF-8");

        response.setContentType("application/json");


        if ("POST".equalsIgnoreCase(request.getMethod()) && defaultFilterProcessUrl.equals(request.getServletPath())) {
            // 验证码验证
            String requestCaptcha = request.getParameter("code");
            String genCaptcha = (String) request.getSession().getAttribute("index_code");
            //String username=request.getParameter ("username");
            //System.out.println (username);

            if (StringUtils.isEmpty(requestCaptcha)) {

                Result result = new Result ();
                result.setFlag (false);
                result.setMessage ("验证码不能为空");
                result.setData (null);
                //httpServletResponse.getWriter().println("{\"code\":403,\"message\":\"小弟弟，你没有权限访问呀！\",\"data\":\"\"}");
                //httpServletResponse.getWriter().println (result);
                //httpServletResponse.getWriter().flush();
                String jsonString = JSON.toJSONString (result);

                //Object toJSON = JSONObject.toJSON (result);
                PrintWriter writer = response.getWriter ();
                writer.append (jsonString);
                writer.flush ();

                return;
            }
            if (!genCaptcha.toLowerCase().equals(requestCaptcha.toLowerCase())) {

                Result result = new Result ();
                result.setFlag (false);
                result.setMessage ("验证码错误");
                result.setData (null);
                //httpServletResponse.getWriter().println("{\"code\":403,\"message\":\"小弟弟，你没有权限访问呀！\",\"data\":\"\"}");
                //httpServletResponse.getWriter().println (result);
                //httpServletResponse.getWriter().flush();
                String jsonString = JSON.toJSONString (result);
                PrintWriter writer = response.getWriter ();
                writer.append (jsonString);
                writer.flush ();

                return;
            }


        }
        chain.doFilter(request, response);

    }

}
